﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using FuelTracker.Models;
using FuelTracker.Services;
using System.Linq;
using Caliburn.Micro;
using System.Diagnostics;
using FuelTracker.Resources;
using Microsoft.Phone.Controls;
using System.Collections.Generic;


namespace FuelTracker.ViewModels
{
    public class FuelLogsViewModel : Screen
    {
        private IStorageService storageService;
        private INavigationService navigationService;
        private ParameterService parameterService;

        private ObservableCollection<FuelLogViewModel> fuelLogs = new ObservableCollection<FuelLogViewModel>();
        public ObservableCollection<FuelLogViewModel> FuelLogs
        {
            get { return fuelLogs; }
        }

        private Vehicle vehicle;

        public string VehicleName
        {
            get
            {
                if (vehicle == null)
                    return "no vehicle selected";

                return vehicle.Name;
            }
        }
       

        public FuelLogsViewModel(IStorageService storageService, INavigationService navigationService, ParameterService parameterService)
        {
            this.storageService = storageService;
            this.navigationService = navigationService;
            this.parameterService = parameterService;
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            vehicle = storageService.GetLastVehicle();

            if (vehicle != null)
            {
                foreach (FuelLog f in vehicle.FuelLogs.OrderByDescending(x => x.Date))
                {
                    fuelLogs.Add(new FuelLogViewModel(f));
                }
            }
        }

        protected override void OnActivate()
        {
            base.OnActivate();

            if (parameterService.HasEntries)
            {
                FuelLog log = parameterService.Pop() as FuelLog;
                fuelLogs.Insert(0, new FuelLogViewModel(log));
            }
       
            NotifyOfPropertyChange(() => VehicleName);
        }

        public void AddNewFuelLog()
        {
            Debug.WriteLine("AddNewFuelLog clicked");
            parameterService.Push(vehicle);
            navigationService.Navigate(Uris.CreateFuelLogView);
        }

        public void NavigateToManageVehicles()
        {
            navigationService.Navigate(Uris.VehiclesView);
        }

        public void NavigateToSettings()
        {

        }

        public void NavigateToAbout()
        {
            Vehicle v = new Vehicle("Alte Schrottkarre")
            {
                FuelLogs = new List<FuelLog>
                {
                    new FuelLog(DateTime.Now.AddDays(-2), 600, 60, 90),
                    new FuelLog(DateTime.Now.AddDays(-3), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-4), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-5), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-6), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-7), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-8), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-9), 550, 60, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-10), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-11), 550, 58, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-12), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-13), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-14), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-15), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-16), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-17), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-18), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-19), 550, 57, 88.50M),
                     new FuelLog(DateTime.Now.AddDays(-2), 600, 60, 90),
                    new FuelLog(DateTime.Now.AddDays(-3), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-4), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-5), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-6), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-7), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-8), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-9), 550, 60, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-10), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-11), 550, 58, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-12), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-13), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-14), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-15), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-16), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-17), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-18), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-19), 550, 57, 88.50M),
                     new FuelLog(DateTime.Now.AddDays(-2), 600, 60, 90),
                    new FuelLog(DateTime.Now.AddDays(-3), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-4), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-5), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-6), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-7), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-8), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-9), 550, 60, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-10), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-11), 550, 58, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-12), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-13), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-14), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-15), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-16), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-17), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-18), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-19), 550, 57, 88.50M),
                     new FuelLog(DateTime.Now.AddDays(-2), 600, 60, 90),
                    new FuelLog(DateTime.Now.AddDays(-3), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-4), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-5), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-6), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-7), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-8), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-9), 550, 60, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-10), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-11), 550, 58, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-12), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-13), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-14), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-15), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-16), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-17), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-18), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-19), 550, 57, 88.50M),
                     new FuelLog(DateTime.Now.AddDays(-2), 600, 60, 90),
                    new FuelLog(DateTime.Now.AddDays(-3), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-4), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-5), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-6), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-7), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-8), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-9), 550, 60, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-10), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-11), 550, 58, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-12), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-13), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-14), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-15), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-16), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-17), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-18), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-19), 550, 57, 88.50M),
                     new FuelLog(DateTime.Now.AddDays(-2), 600, 60, 90),
                    new FuelLog(DateTime.Now.AddDays(-3), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-4), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-5), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-6), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-7), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-8), 500, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-9), 550, 60, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-10), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-11), 550, 58, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-12), 550, 59, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-13), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-14), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-15), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-16), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-17), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-18), 550, 57, 88.50M),
                    new FuelLog(DateTime.Now.AddDays(-19), 550, 57, 88.50M),
                }
            };

            storageService.AddVehicle(v);

        }

        #region Localization
        
        public string LabelApplicationTitleCaps
        {
            get { return Texts.Lbl_ApplicationTitleAllCaps; }
        }

        public string LabelFuelLogs
        {
            get { return Texts.Lbl_FuelLogs; }
        }

        public string LabelGraphic
        {
            get { return Texts.Lbl_Graphic; }
        }

        public string LabelConsumption
        {
            get { return Texts.Lbl_Consumption; }
        }

        public string LabelAdd
        {
            get { return Texts.Lbl_Add; }
        }

        public string LabelPrice
        {
            get { return Texts.Lbl_Price; }
        }

        public string LabelAppBarAdd
        {
            get { return Texts.Lbl_Add; }
        }

        #endregion
    }

}
